package packets.server;

import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;

import javax.imageio.ImageIO;


import server.User;

/**
 * a data structure that is used to be send with a packet
 * it contains the user's details that the server sends in respones for a 'details' request
 */
public class UserDetails implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Certificate _certificate;
	private Integer _id;
	private String _name;
	private String _bday;
	private String _country;
	private String _city;
	private byte[] _img;

	/**
	 * Creates an instance of UserDetails
	 * @param id user's ID
	 * @param user user's details tuple that is held by the server
	 */
	public UserDetails(int id,User user) {
		_certificate = user.getCertificate();
		_id = id;
		_name = user.getName();
		_bday = user.getBday();
		_city = user.getCity();
		_country = user.getCountry();
		_img = user.getPicture();
	}

	/**
	 * 
	 * @return the user's certificate which the server created
	 */
	public Certificate getCertificate() {
		return _certificate;
	}

	/**
	 * 
	 * @return the user's ID
	 */
	public Integer getId() {
		return _id;
	}

	/**
	 * 
	 * @return the user's name
	 */
	public String getName() {
		return _name;
	}

	/**
	 * 
	 * @return the user's birthday
	 */
	public String getBday() {
		return _bday;
	}

	/**
	 * 
	 * @return the user's country
	 */
	public String getCountry() {
		return _country;
	}

	/**
	 * 
	 * @return the user's city
	 */
	public String getCity() {
		return _city;
	}

	/**
	 * a method that creates a file with the user's picture and returns the image it represents
	 * @return the Image that the user sent in the registration process
	 * @throws IOException if there was an error reading/writing from the file
	 */
	public Image getImg() throws IOException {
		try{
			File f = new File("./"+_name);
			f.delete();
			f.createNewFile();
			f.setWritable(true);
			FileOutputStream fios = new FileOutputStream(f);
			fios.write(_img);
			fios.close();
			return ImageIO.read(f);
		}catch(FileNotFoundException e){
			System.out.println("this cant happen");
			e.printStackTrace();
			return null;
		}
	}
}
